﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class UserControl_DanhSachTinRaoVatTheoList : System.Web.UI.UserControl
{
    private int maDanhMucCon = -1;

    public int MaDanhMucCon
    {
        get { return maDanhMucCon; }
        set { maDanhMucCon = value; }
    }
    private bool layTinHot = true;

    public bool LayTinHot
    {
        get { return layTinHot; }
        set { layTinHot = value; }
    }

    private bool layTinThuong = true;

    public bool LayTinThuong
    {
        get { return layTinThuong; }
        set { layTinThuong = value; }
    }

    private int widthGridView = 750;

    public int WidthGridView
    {
        get { return widthGridView; }
        set { widthGridView = value;
        GridView2.Width = value;
        Panel1.Width = value;
        }
    }

    private bool showFooter;

    public bool ShowFooter
    {
        get { return showFooter; }
        set { 
            showFooter = value;
            Panel1.Visible = value;
        }
    }

    private bool showBtnDangBaiRaoVat;

    public bool ShowBtnDangBaiRaoVat
    {
        get { return showBtnDangBaiRaoVat; }
        set { showBtnDangBaiRaoVat = value;
        btnDangBaiRaoVat.Visible = value;
        }
    }

    private List<TIN_RAO_VAT> list = new List<TIN_RAO_VAT>();

    public List<TIN_RAO_VAT> ListTinRaoVat
    {
        get { return list; }
        set { list = value;
        DuaDuLieuVaoBang();
        }
    }

    private bool showHeader = true;

    public bool ShowHeader
    {
        get { return showHeader; }
        set { showHeader = value;
        lblTenMucCon.Visible = value;
        }
    }

    private bool showGridViewHeader = true;

    public bool ShowGridViewHeader
    {
        get { return showHeader; }
        set
        {
            showGridViewHeader = value;
            GridView2.ShowHeader = value;
        }
    }

    private string ten;

    public string Ten
    {
        get { return ten; }
        set
        {
            ten = value;
            lblTenMucCon.Text = value;
        }
    }

    private bool showAutoUpColumn = false;

    public bool ShowAutoUpColumn
    {
        get { return showAutoUpColumn; }
        set { showAutoUpColumn = value;
        GridView2.Columns[6].Visible = value;
        }
    }

    public void Page_Load(object sender, EventArgs e)
    {



     
    }

    public void DuaDuLieuVaoBang()
    {
        if (list.Count() == 0)
        {
            GridView2.DataSource = null;
            GridView2.DataBind();
        }
        else
        {
            ddlKieuSapXep0.Visible = true;
            ddlTenSapXep0.Visible = true;
            btnSapXep0.Visible = true;


            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }



                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
                temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TIN_RAO_VAT objTin = (TIN_RAO_VAT)e.Row.DataItem;
            HyperLink hplTenTin = (HyperLink)e.Row.FindControl("hplTenTin");
            hplTenTin.Text = objTin.TenTinRaoVat;
            hplTenTin.NavigateUrl = "~/RaoVat/ChiTietTinRaoVat.aspx?id=" + objTin.MaTinRaoVat;


            HyperLink hplTenNguoiDung = (HyperLink)e.Row.FindControl("hplTenNguoiDung");
            hplTenNguoiDung.Text = objTin.NGUOI_DUNG.TenNguoiDung;
            hplTenNguoiDung.NavigateUrl = "~/ThongTinLienHeNguoiDung.aspx?id=" + objTin.NGUOI_DUNG.MaNguoiDung;

            Image img = (Image)e.Row.FindControl("Image1");

            if (objTin.MaLoaiTinRaoVat == CMyStaticFunction.layMaTinRaoVatHot())
            {
                
                img.ImageUrl = "~/Images/hot.gif";
            }
            else
            {
                img.Visible = false;
            }

            Image imgSanPham = (Image)e.Row.FindControl("Image2");
            if (objTin.Thumbnail == null)
            {
                HyperLink hp = (HyperLink)e.Row.FindControl("HyperLink1");
                hp.NavigateUrl = @"~/Images/NotImage.jpg";
                imgSanPham.ImageUrl = @"~/Images/NotImage.jpg";
            }
            else
            {
                HyperLink hp = (HyperLink)e.Row.FindControl("HyperLink1");
               
                hp.NavigateUrl = @"~/RaoVat/ShowImage.aspx?id=" + objTin.MaTinRaoVat;
               // hp.NavigateUrl = @"~/Images/NotImage.jpg";
                imgSanPham.ImageUrl = @"~/RaoVat/ShowImage.aspx?id=" + objTin.MaTinRaoVat;

            }

            HyperLink hp2 = (HyperLink)e.Row.FindControl("HyperLink2");

            hp2.NavigateUrl = @"~/Account/AutoUp.aspx?id=" + objTin.MaTinRaoVat;
           
        }
    }
    protected void btnSapXep0_Click(object sender, EventArgs e)
    {
        string TangDan = "Tăng dần";
        string GiamDan = "Giảm dần";
        string TenTinRaoVat = "Tên Tin Rao Vặt";
        string TenNguoiDung = "Tên Người Dùng";
        string ThoiGianDang = "Thời Gian Đăng";
        string SoLanXem = "Số Lần Xem";
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();
        int intId = -1;

        if (Request.QueryString["id"] != null)
        {
            intId = Int32.Parse(Request.QueryString["id"]);
        }
        else
        {
            return;
        }

       
        if (ddlKieuSapXep0.Text == TangDan && ddlTenSapXep0.Text == TenTinRaoVat)
        {
//            var query = from TinRaoVat in linQ.TIN_RAO_VATs where TinRaoVat.MaDanhMucCon == intId && TinRaoVat.Deleted == false orderby TinRaoVat.TenTinRaoVat ascending select TinRaoVat;

            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }



                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
                temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).OrderBy(p => p.TenTinRaoVat).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
        }

        if (ddlKieuSapXep0.Text == GiamDan && ddlTenSapXep0.Text == TenTinRaoVat)
        {
          /* var query = from TinRaoVat in linQ.TIN_RAO_VATs where TinRaoVat.MaDanhMucCon == intId && TinRaoVat.Deleted == false orderby TinRaoVat.TenTinRaoVat descending select TinRaoVat;*/

            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }



                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
                temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).OrderByDescending(p => p.TenTinRaoVat).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
        }

        if (ddlKieuSapXep0.Text == TangDan && ddlTenSapXep0.Text == TenNguoiDung)
        {
            /*var query = from TinRaoVat in linQ.TIN_RAO_VATs where TinRaoVat.MaDanhMucCon == intId && TinRaoVat.Deleted == false orderby TinRaoVat.NGUOI_DUNG.TenNguoiDung ascending select TinRaoVat;*/

            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }



                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
                temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).OrderBy(p => p.NGUOI_DUNG.TenNguoiDung).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
        }

        if (ddlKieuSapXep0.Text == GiamDan && ddlTenSapXep0.Text == TenNguoiDung)
        {
    /*        var query = from TinRaoVat in linQ.TIN_RAO_VATs where TinRaoVat.MaDanhMucCon == intId && TinRaoVat.Deleted == false orderby TinRaoVat.NGUOI_DUNG.TenNguoiDung descending select TinRaoVat;*/

            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }



                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
                temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).OrderByDescending(p => p.NGUOI_DUNG.TenNguoiDung).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
        }

        if (ddlKieuSapXep0.Text == TangDan && ddlTenSapXep0.Text == ThoiGianDang)
        {
       /*     var query = from TinRaoVat in linQ.TIN_RAO_VATs where TinRaoVat.MaDanhMucCon == intId && TinRaoVat.Deleted == false orderby TinRaoVat.ThoiGianDang ascending select TinRaoVat;*/

            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }



                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
                temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).OrderBy(p => p.ThoiGianDang).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
        }

        if (ddlKieuSapXep0.Text == GiamDan && ddlTenSapXep0.Text == ThoiGianDang)
        {
            /*var query = from TinRaoVat in linQ.TIN_RAO_VATs where TinRaoVat.MaDanhMucCon == intId && TinRaoVat.Deleted == false orderby TinRaoVat.ThoiGianDang descending select TinRaoVat;*/

            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }



                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
                temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).OrderByDescending(p => p.ThoiGianDang).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
        }

        if (ddlKieuSapXep0.Text == TangDan && ddlTenSapXep0.Text == SoLanXem)
        {
           /* var query = from TinRaoVat in linQ.TIN_RAO_VATs where TinRaoVat.MaDanhMucCon == intId && TinRaoVat.Deleted == false orderby TinRaoVat.SoLanXem ascending select TinRaoVat;*/

            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }



                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
                temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).OrderBy(a => a.SoLanXem).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
        }

        if (ddlKieuSapXep0.Text == GiamDan && ddlTenSapXep0.Text == SoLanXem)
        {
           /* var query = from TinRaoVat in linQ.TIN_RAO_VATs where TinRaoVat.MaDanhMucCon == intId && TinRaoVat.Deleted == false orderby TinRaoVat.SoLanXem descending select TinRaoVat;*/

            if (LayTinHot || LayTinThuong)
            {
                int maTinHot = -1;
                int maTinThuong = -1;
                if (LayTinHot)
                {
                    maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
                }

                if (LayTinThuong)
                {
                    maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
                }


                List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
               temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).OrderByDescending(p => p.SoLanXem).ToList();

                if (temp != null && temp.Count > 0)
                {
                    GridView2.DataSource = temp;


                    GridView2.DataBind();
                }
            }
            
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
       

            

        if (LayTinHot || LayTinThuong)
        {
            int maTinHot = -1;
            int maTinThuong = -1;
            if (LayTinHot)
            {
                maTinHot = CMyStaticFunction.layMaTinRaoVatHot();
            }

            if (LayTinThuong)
            {
                maTinThuong = CMyStaticFunction.layMaTinRaoVatThuong();
            }

            List<TIN_RAO_VAT> temp = new List<TIN_RAO_VAT>();
            temp = list.Where(p => p.MaLoaiTinRaoVat == maTinHot || p.MaLoaiTinRaoVat == maTinThuong && p.ViPham == false && p.Deleted == false).ToList();

            if (temp != null && temp.Count > 0)
            {
                GridView2.DataSource = temp;


           GridView2.DataBind();
            }
           
        }
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView2_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}
